package com.authority.utils;

import org.apache.commons.lang.math.NumberUtils;

public class TokenUtils {

	/**
	 * 生成token
	 * @param userId
	 * @param warehouseId
	 * @param account
	 * @return
	 * @throws Exception
	 */
	public static String generateToken(String userId,String account,String type) throws Exception{
		return  DES3Utils.encryptMode(userId + "_" + account + "_" +type);
	}
	
	/**
	 * 获取用户id
	 * 
	 * @param token
	 * @return
	 * @throws Exception
	 */
	public static Long getUid(String token)  {
		try {
			String text = DES3Utils.decryptMode(token);
			return NumberUtils.toLong(text.substring(0, text.indexOf("_")));
		} catch (Exception e) {
			return null;
		}
		
	}

	public static void checkToken(String token) throws Exception {

		DES3Utils.decryptMode(token);
	}

	public static String getAccount(String token) {
		
		try {
			String text = DES3Utils.decryptMode(token);
			
			return text.substring(text.indexOf("_") + 1, text.lastIndexOf("_"));
		} catch (Exception e) {
			return null;
		}
	}
	
	public static Integer getType(String token) {
		
		try {
			String text = DES3Utils.decryptMode(token);
			
			return NumberUtils.toInt(text.substring(text.lastIndexOf("_") + 1, text.length()));
		} catch (Exception e) {
			return null;
		}
	}
	
	
	public static void main(String[] args) throws Exception {

	
		String token = 	generateToken("1",null,null);
		System.out.println(token);
		System.out.println(DES3Utils.decryptMode(token));
		System.out.println(getUid(token));
		System.out.println(getAccount(token));
		System.out.println(getType(token));
	}
}
